package com.zlx.guguanjia.controller;

import com.github.pagehelper.PageInfo;
import com.zlx.guguanjia.entity.Result;
import com.zlx.guguanjia.entity.SysArea;
import com.zlx.guguanjia.service.SysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author 周罗校
 * @Description
 * @ClassName SysAreaController
 * @date 2020.10.30 13:44:22
 * 该版权归周罗校所有，最终解释权以官方为准
 */
@RestController
@RequestMapping("/manager/area/")
public class SysAreaController {
    @Autowired
    private SysAreaService service;


    @GetMapping("selectPage/{start}/{pageSize}")
    public Result selectPage(@PathVariable("start") int start, @PathVariable("pageSize") int pageSize, @RequestParam Map<String, Object> condition) {
        PageInfo<SysArea> sysAreaPageInfo = service.selectPage(start, pageSize, condition);
        return new Result(sysAreaPageInfo);
    }

    @GetMapping("selectAll")
    public Result selectAll() {
        List<SysArea> sysAreaPageInfo = service.selectAll();
        return new Result(sysAreaPageInfo);
    }

    @PutMapping("updateInfoById")
    public Result updateInfoById(@RequestBody SysArea area) {
        return new Result(service.updateByPrimaryKeySelective(area));
    }

    //逻辑删除
    @PutMapping("deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        SysArea area = new SysArea();
        area.setId(id);
        //标记删除了
        area.setDelFlag("1");
        area.setUpdateDate(new Date());
        return new Result(service.updateByPrimaryKeySelective(area));
    }

    @GetMapping("getById/{id}")
    public Result getById(@PathVariable("id") Long id) {
        SysArea sysAreas = service.selectById(id);
        return new Result(sysAreas);
    }

    @PostMapping("add")
    public Result add(@RequestBody SysArea area) {
        area.setDelFlag("0");
        area.setCreateDate(new Date());
        area.setUpdateDate(new Date());
        SysArea parent = service.selectByPrimaryKey(area.getParentId());
        area.setParentIds(parent.getParentIds() + area.getParentId() + ",");
        return new Result(service.insertSelective(area));
    }

    //excel的批量插入
    @PostMapping("upload")
    public Result upload(@RequestBody MultipartFile file) {
        Result result = new Result();
        try {
            service.upload(file.getInputStream());
            result.setMsg("上传成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("download")
    public void download(HttpServletResponse response) {
        //设置响应头，只能以副本的格式下载，浏览器不能解析
        try {
            //response.setHeader("Content-Disposition","attachment;filename="+new String("区域信息.xlsx".getBytes(),"iso8859-1"));
            response.setHeader("content-disposition", "attachment;filename=" + new String("区域信息.xlsx".getBytes(), "iso8859-1"));
            service.download(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
